import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { UtilService } from '@core/services/util.service';

@Component({
  selector: 'app-dic-edit',
  templateUrl: './dic-edit.component.html',
  styles: []
})
export class DicEditComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf: SFComponent;
  @Input()type: 0 | 1;
  @Input()
    set _param(value){
      if(!value) return
      this.getDetail(value.id)
      // this.param = value
      // let list=[]
      // for(let item of value.categoryCode){ 
      //   list.push(item.code)
      // }
      // this.param.categoryCode = list
    }
  param:any
  schema: SFSchema = {
    properties: {
      categoryCode: {
        type: 'string',
        title: '收费类目',
        enum:[],
        ui: {
          widget: 'select',
          mode: 'multiple',
          dropdownStyle: {
            'max-height': '300px'
          }
        }
      },
      userId: {
        type: 'string',
        title: '用户',
        enum:[],
        ui: {
          widget: 'select',
          dropdownStyle: {
            'max-height': '300px'
          }
        }
      },
      // shineField: {
      //   type: 'string',
      //   title: '是否缴费',
      //   enum: ['不缴费', '缴费'],
      //   ui: {
      //     widget: 'radio',
      //   },
      //   default: '不缴费',
      // },
      clientType: {
        type: 'string',
        title: '缴费端',
        // enum: ['PC端+小程序端', 'PC端', '小程序端'],
        enum: [
          {label:"PC端+小程序端",value:0},
          {label:"PC端",value:1},
          {label:"小程序端",value:2},
        ],
        ui: {
          widget: 'radio',
        },
        default:0,
      },
    },
    required: ["categoryCode"]
  }
  userId: any;
  constructor(
    private api: ApiService,
    private http: _HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private util:UtilService
  ) { }

  ngOnInit() {
    this.getCategory()
    this.getUserList()
  }
  submit(data: any): void {
    const url = this.type === 0 ? "/system/user/fee/save" : "/system/user/fee/update";
    this.http.request<any>("post", url, {body:data}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.msg.success('操作成功')
        this.modalRef.destroy('ok')
      } else {
        this.msg.error(res.message)
      }
    })

  }

  //获取收费类目列表
  getCategory(){
    this.http.request<any>("get", "/system/user/fee/getCategoryList", {}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.schema.properties.categoryCode.enum = res.data.map(item => {
          return {label: item.name, value: item.code}
        })
        this.sf.refreshSchema();
      } else {
        this.msg.error(res.message)
      }
    })
  }
  //获取用户列表
  getUserList(){
    this.http.request<any>("post", "/system/user/list", {body:{}}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.schema.properties.userId.enum = res.data.map(item => {
          return {label: item.userName, value: item.id}
        })
        this.sf.refreshSchema();
      } else {
        this.msg.error(res.message)
      }
    })
  }
  //查询详情
  getDetail(id){
    this.http.request<any>("get", "/system/user/fee/findById?id="+id, {}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.param = res.data
        let list=[]
        for(let item of res.data.categoryVoList){ 
          list.push(item.code)
        }
        this.param.categoryCode = list
      } else {
        this.msg.error(res.message)
      }
    })
  }
}
